<?php

namespace app\modules\web\controllers;

use yii\web\Controller;
use \app\common\services\UrlService;
use \app\models\User;
use \app\modules\web\controllers\common\BaseController;
class UserController extends BaseController
{
    
    //用户登录
    public function actionLogin()
    {
        if(\Yii::$app->request->isGet){
        	$this->layout=false;
            return $this->render('login');
        }
        //登录处理
        $login_name=trim($this->post("login_name",""));
        $login_pwd=trim($this->post("login_pwd",""));
        if(!$login_name || !$login_pwd){
            return $this->renderJs("用户名或者密码不能为空-1",UrlService::buildWebUrl('/user/login'));
        }
        //检查登录名,密码
        $user_info=User::find()->where(["login_name"=>$login_name])->one();//查看是否存在
        if(!$user_info){
            return $this->renderJs("用户名或者密码错误-2",UrlService::buildWebUrl('/user/login'));

        }
        if( md5($login_pwd.md5($user_info['login_salt']))!=$user_info['login_pwd']){
            return $this->renderJs("用户名或者密码错误-3",UrlService::buildWebUrl('/user/login'));
        }
        //保存cookies
        
        $this->setLoginStatus($user_info);

        return $this->redirect(UrlService::buildWebUrl('/dashbord/index'));
    }
    //编辑
    public function actionEdit()
    {
    	if(\Yii::$app->request->isGet){
            $this->layout="main";
            return $this->render('edit',["user_info"=>$this->current_user]);
        }
        $nickname=trim($this->post("nickname",""));
        $email=trim($this->post("email",""));
        
        if(mb_strlen($nickname,"utf-8")<1){
            return $this->renderJson([],"请输入合法的用户名---",-1);
        }
        if(mb_strlen($email,"utf-8")<1){
            return $this->renderJson([],"请输入合法的用户名--",-1);
        }
        $user_info=$this->current_user;
        $user_info->nickname=$nickname;
        $user_info->email=$email;
        $user_info->updated_time=date("Y-m-d H:i:s");
        $user_info->update(0);
        return $this->renderJson([],"编辑成功--");
        
    }
    //重置登录密码
    public function actionReset_pwd()
    {
        if(\Yii::$app->request->isGet){
            $this->layout="main";
            return $this->render('reset_pwd');
        }
        $user_info=$this->current_user;
        $old_password=$this->post("old_password","");
    	$new_password=$this->post("new_password","");
        $user_info=$this->current_user;

        if(mb_strlen($old_password,"utf-8")<1){
            return $this->renderJson([],"请输入旧密码",-1);
        }
        if(mb_strlen($new_password,"utf-8")<6){
            return $this->renderJson([],"新密码不能小于6",-1);
        }
        if($new_password==$old_password){
            return $this->renderJson([],"新密码不能旧密码相同",-1);
        }
        //检查旧密码是否正确
        $user_info=$this->current_user;
        
        if(!$user_info->verifyPwd($old_password)){
            return $this->renderJson([],"旧密码错误",-1);

        }
        $user_info->login_pwd=$user_info->setPwd($new_password);
        $user_info->updated_time=date("Y-m-d H:i:s");
        $user_info->update(0);
        $this->setLoginStatus($user_info);
        return $this->renderJson([],"编辑成功--");
    }
    //退出
    public function actionLogout(){
        $this->removeLogin();
        return $this->redirect(UrlService::buildWebUrl('/user/login'));
    }
}
